﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Navigation;
using WPF.MDI;
using System.Xml;
using Com.DA;
using Com.BU;
using System.Diagnostics;

namespace Com.GUI.LibraryManager
{
    /// <summary>
    /// Interaction logic for LibraryMainGUI.xaml
    /// </summary>
    
    public partial class LibraryMainGUI : Window
    {
        public LibraryMainGUI()
        {
            InitializeComponent();

      
        }

        bool blMultiChild = false;// Điều chỉnh chạy đơn luồng hay đa luồng
        private void btnProductColdStorage_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            OpenColdStorageProduct();

        }

        private void OpenColdStorageProduct()
        {
            //Tắt chạy đa luồng- đang chạy đơn luồng
            if (blMultiChild == false) { Container.Children.Clear(); }
            // Mở cửa sổ con
            Container.Children.Add(new MdiChild
            {
                Title = "Cold Storage's product",
                Content = new ucColdStorageProduct(),
                Width = 560,
                Height = 440,
                Resizable = false,
                Position = new Point(300, 80)
            });
            //Xắp xếp đầu hàng khi chạy đơn luồng
            if (blMultiChild == false) { Container.MdiLayout = MdiLayout.Cascade; }
        }
        private void btnLocationColdStorage_Click(object sender, System.Windows.RoutedEventArgs e)
        {

            OpenColdStorageLocation();
        }

        private void OpenColdStorageLocation()
        {
            // On or off MultiChild on Container
            if (blMultiChild == false) { Container.Children.Clear(); }
            Container.Children.Add(new MdiChild
            {
                Title = "Location",
                Content = new ucColdStorageLocation(),
                Width = 450,
                Height = 330,
                Resizable = false,
                Position = new Point(300, 80)
            });
            //Xắp xếp đầu hàng khi chạy đơn luồng
            if (blMultiChild == false) { Container.MdiLayout = MdiLayout.Cascade; }
        }

        private void btnWallColdStorage_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            OpenColdStorageWall();
        }

        private void OpenColdStorageWall()
        {
            // On or off MultiChild on Container
            if (blMultiChild == false) { Container.Children.Clear(); }
            Container.Children.Add(new MdiChild
            {
                Title = "Wall In Cold Storage",
                Content = new ucColdStorageWall(),
                Width = 450,
                Height = 230,
                Resizable = false,
                Position = new Point(300, 80)
            });
            //Xắp xếp đầu hàng khi chạy đơn luồng
            if (blMultiChild == false) { Container.MdiLayout = MdiLayout.Cascade; }
        }

        private void btnWrappingColdStorage_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            OpenColdStorageWrapping();
        }

        private void OpenColdStorageWrapping()
        {
            // On or off MultiChild on Container
            if (blMultiChild == false) { Container.Children.Clear(); }
            Container.Children.Add(new MdiChild
            {
                Title = "Wrapping In Cold Storage",
                Content = new ucColdStorageWrapping(),
                Width = 450,
                Height = 230,
                Resizable = false,
                Position = new Point(300, 80)
            });
            //Xắp xếp đầu hàng khi chạy đơn luồng
            if (blMultiChild == false) { Container.MdiLayout = MdiLayout.Cascade; }
        }

        private void btnAirWall_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            OpenAirWall();

        }

        private void OpenAirWall()
        {
            if (blMultiChild == false) { Container.Children.Clear(); }
            Container.Children.Add(new MdiChild
            {
                Title = "Wall in Air condition",
                Content = new ucAirWall(),
                Width = 450,
                Height = 330,
                Resizable = false,
                Position = new Point(300, 80)
            });
            //Xắp xếp đầu hàng khi chạy đơn luồng
            if (blMultiChild == false) { Container.MdiLayout = MdiLayout.Cascade; }
        }

        private void btnRoof_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            OpenAirRoof();

        }

        private void OpenAirRoof()
        {
            // TODO: Add event handler implementation here.
            if (blMultiChild == false) { Container.Children.Clear(); }
            Container.Children.Add(new MdiChild
            {
                Title = "Roof",
                Content = new ucAirRoof(),
                Width = 450,
                Height = 230,
                Resizable = false,
                Position = new Point(300, 80)
            });
            //Xắp xếp đầu hàng khi chạy đơn luồng
            if (blMultiChild == false) { Container.MdiLayout = MdiLayout.Cascade; }
        }
        private void btnGlass_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            OpenAirGlass();
        }

        private void OpenAirGlass()
        {
            // TODO: Add event handler implementation here.
            if (blMultiChild == false) { Container.Children.Clear(); }
            Container.Children.Add(new MdiChild
            {
                Title = "Glass",
                Content = new ucAirGlass2(),
                Width = 441,
                Height = 287,
                Resizable = false,
                Position = new Point(300, 80)
            });
            //Xắp xếp đầu hàng khi chạy đơn luồng
            if (blMultiChild == false) { Container.MdiLayout = MdiLayout.Cascade; }
        }

        private void btnCeiling_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            OpenAirCeiling();
        }

        private void OpenAirCeiling()
        {
            // TODO: Add event handler implementation here.
            if (blMultiChild == false) { Container.Children.Clear(); }
            Container.Children.Add(new MdiChild
            {
                Title = "Ceiling",
                Content = new ucAirCeiling(),
                Width = 450,
                Height = 400,
                Resizable = false,
                Position = new Point(300, 80)
            });
            //Xắp xếp đầu hàng khi chạy đơn luồng
            if (blMultiChild == false) { Container.MdiLayout = MdiLayout.Cascade; }
        }

        private void btnSpace_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            OpenAirSpace();

        }

        private void OpenAirSpace()
        {
            // TODO: Add event handler implementation here.
            if (blMultiChild == false) { Container.Children.Clear(); }
            Container.Children.Add(new MdiChild
            {
                Title = "Aircodition Space",
                Content = new ucAirSpace(),
                Width = 450,
                Height = 480,
                Resizable = false,
                Position = new Point(300, 80)
            });
            //Xắp xếp đầu hàng khi chạy đơn luồng
            if (blMultiChild == false) { Container.MdiLayout = MdiLayout.Cascade; }
        }

        private void btnAirFlow_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            OpenAirFlow();
        }

        private void OpenAirFlow()
        {
            // TODO: Add event handler implementation here.
            if (blMultiChild == false) { Container.Children.Clear(); }
            Container.Children.Add(new MdiChild
            {
                Title = "Air Flow",
                Content = new ucAirFlow(),
                Width = 450,
                Height = 230,
                Resizable = false,
                Position = new Point(300, 80)
            });
            //Xắp xếp đầu hàng khi chạy đơn luồng
            if (blMultiChild == false) { Container.MdiLayout = MdiLayout.Cascade; }
        }

        /// <summary>
        /// Viewer 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void mniViewCascade_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            Container.MdiLayout = MdiLayout.Cascade;
            mniViewHorizontally.IsChecked = false;
            mniViewVertically.IsChecked = false;
        }

        private void mniViewHorizontally_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            Container.MdiLayout = MdiLayout.TileHorizontal;
            mniViewVertically.IsChecked = false;
            mniViewCascade.IsChecked = false;
        }

        private void mniViewVertically_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            Container.MdiLayout = MdiLayout.TileVertical;
            mniViewHorizontally.IsChecked = false;
            mniViewCascade.IsChecked = false;
        }

        private void mniCloseAll_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            Container.Children.Clear();
        }

        private void mniLibraryLocation_Click(object sender, RoutedEventArgs e)
        {

            // On or off MultiChild on Container
            if (blMultiChild == false) { Container.Children.Clear(); }
            Container.Children.Add(new MdiChild
            {
                Title = "Location",
                Content = new ucColdStorageLocation(),
                Width = 450,
                Height = 330,
                Resizable = false,
                Position = new Point(300, 80)
            });
            //Xắp xếp đầu hàng khi chạy đơn luồng
            if (blMultiChild == false) { Container.MdiLayout = MdiLayout.Cascade; }
        }

        private void mniCSLocation_Click(object sender, RoutedEventArgs e)
        {
            OpenColdStorageLocation();
        }

        private void mniCSProduct_Click(object sender, RoutedEventArgs e)
        {
            OpenColdStorageProduct();
        }

        private void mniCSWall_Click(object sender, RoutedEventArgs e)
        {
            OpenColdStorageWall();
        }

        private void mniCSWrapping_Click(object sender, RoutedEventArgs e)
        {
            OpenColdStorageWrapping();
        }

        private void mniACRoof_Click(object sender, RoutedEventArgs e)
        {
            OpenAirRoof();
        }

        private void mniACGlass_Click(object sender, RoutedEventArgs e)
        {
            OpenAirGlass();
        }

        private void mniACCeiling_Click(object sender, RoutedEventArgs e)
        {
            OpenAirCeiling();
        }

        private void mniACSpace_Click(object sender, RoutedEventArgs e)
        {
            OpenAirSpace();
        }

        private void mniACAirFlow_Click(object sender, RoutedEventArgs e)
        {
            OpenAirFlow();
        }

        private void mniACWall_Click(object sender, RoutedEventArgs e)
        {
            OpenAirWall();
        }

        private void mniLocation_Click(object sender, RoutedEventArgs e)
        {
            OpenColdStorageLocation();
        }

        private void mniViewSingleChild_Click(object sender, RoutedEventArgs e)
        {
            blMultiChild = false;
            mniViewMultiChild.IsChecked = false;
        }

        private void mniViewMultiChild_Click(object sender, RoutedEventArgs e)
        {
            blMultiChild = true;
            mniViewSingleChild.IsChecked = false;
        }

        private void mniFileExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void mniFileImport_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();

            ofd.Filter = "Data Source (*.txt, *.xml)|*.txt;*.xml";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string filePath = ofd.FileName;
                if (MessageBox.Show("Do you really want to import this library data?", "Confirm Import", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    Com.ImportLibraryGUI import = new Com.ImportLibraryGUI();
                    import.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                    import.Topmost = true;
                    this.IsEnabled = false;
                    import.Show();

                }

            }

        }

        private void mniFileExport_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.SaveFileDialog osd = new System.Windows.Forms.SaveFileDialog();
            osd.Filter = "Data Source (*.xml)|*.xml";
            if (osd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string filePath = osd.FileName;
                if (writeXMLFile(filePath))
                {
                    MessageBox.Show("Write XML file successfully!");
                }

            }
        }

        public bool writeXMLFile(string fileName)
        {
            try
            {
                LibraryManagerInCom.loadLocation();
                XmlTextWriter writer = new XmlTextWriter(fileName, System.Text.Encoding.UTF8);
                writer.WriteStartDocument(true);
                //writer.Formatting = Formatting.Indented;
                //writer.Indentation = 2;
                writer.WriteStartElement("tb_location");
                foreach (var lc in LibraryManagerInCom.LocationList)
                {
                    createNode(lc.location, lc.max_temperature.ToString(), lc.average_temperature.ToString(), writer);
                }
                writer.WriteEndElement();// tb_location


                writer.WriteStartElement("tb_wrapping");
                LibraryManagerInCom.loadWrapping();
                foreach (var item in LibraryManagerInCom.WrappingList)
                {

                }
                writer.WriteEndElement();// tb_wrapping
                writer.WriteEndDocument();
                writer.Close();
                Process.Start(fileName);
                return true;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        private void creaNode2(string table, string[] column, XmlTextWriter writer)
        {
            writer.WriteStartElement(table);

            for (int i = 0; i < column.Length; i++)
            {

            }

            writer.WriteEndElement();

        }
        private void createNode(string lc, string max_temp, string avg_temp, XmlTextWriter writer)
        {
            writer.WriteStartElement("Location");
            writer.WriteStartElement("Location");
            writer.WriteString(lc);
            writer.WriteEndElement();
            writer.WriteStartElement("Max_Temper");
            writer.WriteString(max_temp);
            writer.WriteEndElement();
            writer.WriteStartElement("Avg_Temper");
            writer.WriteString(avg_temp);
            writer.WriteEndElement();
            writer.WriteEndElement();
        }


    }


}
